package com.huilink.webmail.util.client.widgets;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SourcesChangeEvents;
import com.google.gwt.user.client.ui.Widget;

public abstract class InputPanel extends Composite {
	
	private boolean required;

	private HorizontalPanel panel = new HorizontalPanel();
	private Label requiredLabel = new Label("");
	private Label label = new Label();
	
	private Label error = new Label();
	
	private List validatorList = new ArrayList();
	
	private FocusWidget inputWidget;
	
	private String description;
	private String originValue;
	
	//must be called.
	protected void initInputPanel(){
		setStyle();
		
		panel.add(requiredLabel);
		panel.setCellWidth(requiredLabel, "8px");
		label.setText(description);
		panel.add(label);
		
		panel.add(inputWidget);
		panel.add(error);
		
		initWidget(panel);
		
		addChangeListener(new ChangeListener(){
			public void onChange(Widget sender) {
				error.setText("");
			}
		});
		addFocusListener(new FocusListener(){
			public void onFocus(Widget sender) {
			}
			public void onLostFocus(Widget sender) {
				validate();
			}
		});
	}
	
	public void setDescriptionWidth(String w){
		label.setWidth(w);
		//panel.setCellWidth(label, w);
	}
	
	protected void setStyle(){
		panel.setSpacing(3);
		panel.setStyleName("input-panel");
		requiredLabel.setStyleName("input-panel-error");
		label.setStyleName("input-panel-label");
		error.setStyleName("input-panel-error");
	}
	
	public void setError(String errorMsg){
		error.setText(errorMsg);
	}
	
	public boolean validate(){
		String value = getValue();
		if(required){
			if(value == null || value.length()== 0 ){
				error.setText("Required");
				return false;
			}
		}
		
		for(int i=0; i<validatorList.size(); i++){
			Validator validator = (Validator)validatorList.get(i);
			if(!validator.validate(value)){
				error.setText(validator.getErrorMessage());
				return false;
			}
		}
		error.setText(null);
		return true;
	}

	protected String getOriginValue() {
		return originValue;
	}

	protected void setOriginValue(String value) {
		this.originValue = value;
	}
	
	protected void setDescription (String description){
		this.description = description;
	}

	public void setRequired(boolean required) {
		this.required = required;
		if(required)
			requiredLabel.setText("*");
		else
			requiredLabel.setText("");
	}
	
	public void addFocusListener(FocusListener listener){
		inputWidget.addFocusListener(listener);
	}
	
	public void addValidator(Validator validator){
		validatorList.add(validator);
	}
	
	protected void setInputWidget(FocusWidget widget){
		this.inputWidget = widget;
	}
	
	
	public abstract void setValue(String value);
	
	public abstract String getValue();
	
	public abstract void reset();

	public void addChangeListener(ChangeListener listener){
		SourcesChangeEvents sce = (SourcesChangeEvents)inputWidget;
		sce.addChangeListener(listener);
	}
	
	public void addKeyboardListener(KeyboardListener listener){
		inputWidget.addKeyboardListener(listener);
	}
}
